<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
	<th:block th:include="include :: header('新增书籍')" />
	<th:block th:include="include :: select2-css" />
</head>
<body>
    <div class="main-content">
        <form id="form-book-add" class="form-horizontal">
            <h4 class="form-header h4">基本信息</h4>
            <div class="row">
            	<div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-4 control-label is-required">章节名称：</label>
                        <div class="col-sm-8">
                            <input name="chapterName" id="chapterName" placeholder="请输入章节名称" class="form-control" type="text" maxlength="30" required>
                        </div>
                    </div>
                </div>
                <div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-4 control-label">排序：</label>
                        <div class="col-sm-8">
                            <div class="input-group">
                                <input id="orderNum" name="orderNum" class="form-control" type="text" maxlength="50" required>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-4 control-label is-required">上传文件：</label>
                        <div class="col-sm-8">
                            <input id="file" name="file"  class="form-control" type="file" onchange="loadFile(this)" accept=".pdf,.txt">
                        </div>
                    </div>
                </div>
                <div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-4 control-label">预计学习时间(秒)：</label>
                        <div class="col-sm-8">
                            <div class="input-group">
                                <input id="readTime" name="readTime" class="form-control" type="text" maxlength="50" disabled>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

        </form>
    </div>
      
    <div class="row">
        <div class="col-sm-offset-5 col-sm-10">
            <button type="button" class="btn btn-sm btn-primary" onclick="submitHandler()"><i class="fa fa-check"></i>保 存</button>&nbsp;
            <button type="button" class="btn btn-sm btn-danger" onclick="closeItem()"><i class="fa fa-reply-all"></i>关 闭 </button>
        </div>
    </div>
	<th:block th:include="include :: footer" />
	<th:block th:include="include :: select2-js" />
	<script>
	    var prefix = ctx + "chapter";
        var bookId = [[${bookId}]];

        function submitHandler() {
	        if ($.validate.form()) {
	        	var formData = new FormData();
	        	formData.append('chapterName', $("#chapterName").val());
                formData.append('orderNum', $("#orderNum").val());
                formData.append('readTime', $("#readTime").val());
                formData.append('bookId', bookId);
                //添加文件
                formData.append('file', $('#file')[0].files[0]);
                $.ajax({
                    url: prefix + "/addCha",
                    type: 'post',
                    cache: false,
                    data: formData,
                    processData: false,
                    contentType: false,
                    dataType: "json",
                    success: function(result) {
                        $.operate.successCallback(result);
                    }
                });
	        }
	    }
         

		function doSubmit(index, layero){
			var tree = layero.find("iframe")[0].contentWindow.$._tree;
			var body = layer.getChildFrame('body', index);
   			$("#treeId").val(body.find('#treeId').val());
   			$("#treeName").val(body.find('#treeName').val());
   			layer.close(index);
		}

		function loadFile(file){
            var filePath = file.value;
            var fileExt = filePath.substring(filePath.lastIndexOf("."))
                .toLowerCase();
            console.log(fileExt);
            if(file.files[0]){
                var fileSize = (file.files[0].size / 1024).toFixed(0);
                //判断文件类型
                if (fileExt.match(/.txt/i)) {
                    //如果是文本资料,1kb预计需要10s学完
                    $("#readTime").val(fileSize * 10);
                }else if(fileExt.match(/.pdf/i)){
                    //如果是pdf资料,1kb预计需要1学完
                    $("#readTime").val(fileSize);
                }
            }

        }
    </script>
</body>
</html>